package com.lanting.meetingserver.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lanting.meetingserver.dao.UserDao;
import com.lanting.meetingserver.domain.User;
import com.lanting.meetingserver.query.UserQuery;
import com.lanting.meetingserver.util.Common;
import com.lanting.meetingserver.util.RecordList;

@Service
public class UserServiceImpl {

	@Resource
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	
	public User login(String loginId,String password){
		return userDao.getUserByLoginIdAndPassord(loginId, password);
	}
	
	public User getUserByLoginId(String loginId){
		User user = new User();
		user = userDao.getUserByLoginId(loginId);
		return user;
	}
	
	public User get(int id){
		return userDao.getUserById(id);
	}
	
	public List<User> getListenersByMeetingId(int meetingId){
		List<User> list = userDao.getListenersByMeetingId(meetingId);
		return list;
	}
	
	public List<User> getSpeakersByMeetingId(int meetingId){
		List<User> list = userDao.getSpeakersByMeetingId(meetingId);
		return list;
	}
	
	public RecordList<User> getList(UserQuery query){
		int count = userDao.getCount();
		List<User> data = userDao.getList(query);
		RecordList<User> list = new RecordList<User>();
		list.Data = data;
		list.pageindex = query.pageindex;
		list.pagesize = query.pagesize;
		list.recordcount = count;
		return list;
	}
	
	public RecordList<User> getMeetingUserList(UserQuery query){
		int count = userDao.getMeetingUserListCount(query);
		List<User> data = userDao.getMeetingUserList(query);
		RecordList<User> list = new RecordList<User>();
		list.Data = data;
		list.pageindex = query.pageindex;
		list.pagesize = query.pagesize;
		list.recordcount = count;
		return list;
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void delete(int userId) {
		User user = userDao.getUserById(userId);
		if (user == null) throw new RuntimeException("要删除的用户不存在!");
		try {
			userDao.delete(user);
		} catch (Throwable e) {
			throw new RuntimeException("要删除的用户在系统中存在关联数据，请先删除相关的数据，然后再尝试删除！");
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void resetPassword(int userId,String newPassword) {
		User user = userDao.getUserById(userId);
		if (user == null) throw new RuntimeException("要修改密码的用户不存在!");
		try {
			user.setUserPassword(Common.getSecurityPassword(newPassword,user.getLoginId()));
			userDao.update(user);
		} catch (Throwable e) {
			throw new RuntimeException("密码更新失败！");
		}
	}
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int save(User user) {
		User userInDB = null;
		if (user.getUserId() > 0) {
			userInDB = userDao.getUserById(user.getUserId());
			if (StringUtils.isNotBlank(user.getUserPassword())) {
				userInDB.setUserPassword(Common.getSecurityPassword(user.getUserPassword(),user.getLoginId()));
			}
		} else {
			// 创建新用户
			User tmpUser = userDao.getUserByLoginId(user.getLoginId());
			List<User> tmpLististUser = userDao.getUserByEmail(user.getUserEmail());
			if (tmpUser !=null || tmpLististUser.size()>0) {
				throw new RuntimeException("您所添加的用户，登录名或邮箱已经注册过，请您选择该用户或者重新创建用户！");
			}
			userInDB = new User();
			userInDB.setLoginId(user.getLoginId());
			
			userInDB.setModifier(user.getModifier());
			if (StringUtils.isNotBlank(user.getUserPassword())) {
				userInDB.setUserPassword(Common.getSecurityPassword(user.getUserPassword(),user.getLoginId()));
			} else {
				userInDB.setUserPassword(Common.getSecurityPassword("000000",user.getLoginId()));
			}
		}
		userInDB.setUserEmail(user.getUserEmail());
		userInDB.setUserName(user.getUserName());
		userInDB.setModifier(user.getModifier());
		userInDB.setModifyDate(new Date());
		userInDB.setUserPosition(user.getUserPosition());
		
		if (userInDB.getUserId() > 0) {
			userDao.update(userInDB);
		} else {
			userDao.insert(userInDB);
		}
		
		return userInDB.getUserId();
	}
}